<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class User extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('account_model');
        $this->load->model('finance_list_model');
        $this->load->model('experience_list_model');
        $this->load->model('withdraw_order_model');
        $this->load->model('withdraw_list_model');
        $this->load->model('exchange_order_model');
        $this->load->model('exchange_list_model');
        $this->load->model('users_spread_model');
        $this->load->model('plat_award_list_model');
        $this->load->model('plat_model');
        $this->load->model('gift_model');
        $this->load->model('users_log_model');
        $this->load->model('users_bank_model');
        $this->load->model('users_award_notice_model');
        $this->load->model('content_model');
        $this->load->model('cate_model');
        $this->load->model('new_task_model');
        if($_POST){
            if(!$this->userinfo){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
        }
        $this->sso_login();
        $this->friend_flag = array('1'=>'一级好友','2'=>'二级好友','3'=>'三级好友','4'=>'四级好友','5'=>'五级好友');
        
        $this->assign("finance_balance", number_format($this->user_info["balance"]));
        $this->assign("experience_balance", number_format($this->user_info["experience_balance"]));
        $this->assign("friends_balance", number_format($this->users_spread_model->get_user_total_count(array("parent_id"=>$this->userinfo['user_id']))));
    }

    function index() {
        
        //当前月份
        $current_month = date('Y年m月');
        $str_date = '';
        $str_finance_data = '';
        $str_experience_data = '';
        $end_day_time = strtotime(date('Y-m-d').' 00:00:05')-24*6*3600;
        $where['user_id'] = $this->userinfo['user_id'];
        $where['type'] = 1;
        $finance_total = 0;
        $experience_total = 0;
        for($i = 1;$i <= 7;$i++){
            $date = date('Y-m-d',$end_day_time);
            $time = strtotime($date);
            $where['create_time >='] = $time;
            $where['create_time <='] = $time+24*3600;
            //获取当天溜溜币总值
            $finance = $this->finance_list_model->get_in_count_by_time($where);
            $finance['finance'] = $finance['finance'] ? $finance['finance'] : 0;
            //统计总溜溜币
            $finance_total += $finance['finance'];
            //获取当天积分总值
            $experience = $this->experience_list_model->get_in_count_by_time($where);
            $experience['experience'] = $experience['experience'] ? $experience['experience'] : 0;
            //统计总积分值
            $experience_total += $experience['experience'];
            $str_date = $str_date."'".$date."',";
            $str_finance_data = $str_finance_data.$finance['finance'].',';
            $str_experience_data = $str_experience_data.$experience['experience'].',';
            $end_day_time+=24*3600;
        }
        $this->assign('finance_total', number_format($finance_total));
        $this->assign('experience_total', number_format($experience_total));
        $this->assign('date_attribute', $str_date);
        $this->assign('finance_attribute', $str_finance_data);
        $this->assign('experience_attribute', $str_experience_data);
        $this->assign('current_month', $current_month);
        
        //我的推荐项目recommend
        $recommends = $this->plat_model->recommend_plat_list($this->userinfo['user_id']);
        foreach ($recommends as $key=>$val){
            $class_str = $val['company_bg_type'] ? $this->bgType[$val['company_bg_type']]["class"] : $this->bgType[0]['class'];
            $recommends[$key]['class'] = $class_str;
            $recommends[$key]['url'] = site_url('mplat_'.$val['name_en']);
            $recommends[$key]['max_award'] = number_format($val['max_award']);
            $recommends[$key]['add_interest'] = ($val['add_interest'] * 100)."%";
            $recommends[$key]['logo_detail'] = strstr($val['logo_detail'], "http://") ? $val['logo_detail'] : $this->img_url.$val['logo_detail'];
        }
        $this->assign("recommends",$recommends);
        $this->assign("finance_total", number_format($finance_total));
        $this->assign("experience_total", number_format($experience_total));
        $this->display('user/index.html');
    }
    
    function details(){
        $this->assign("end_time",  date("Y-m-d"));
        $this->display('user/details.html');
    }
    //溜溜币明细
    function detail_finance(){
        $type = isset($_REQUEST['type']) ? trim($_REQUEST['type']) : 0;
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $start_date = isset($_REQUEST['start_date']) ? trim($_REQUEST['start_date']) : "";
        $end_date = isset($_REQUEST['end_date']) ? trim($_REQUEST['end_date']) : date("Y-m-d");
        $where = array();
        $where['user_id'] = $this->userinfo['user_id'];
        $type ? $where['type'] = $type : "";
        if($start_date){$where['create_time >='] = strtotime($start_date." 00:00:00");}
        if($end_date){$where['create_time <='] = strtotime($end_date." 23:59:59");} 
        $totalCount = $this->finance_list_model->get_count($where);
        
        $records_per_page = 5;
        $list = $this->finance_list_model->get_list($where,$pageIndex * $records_per_page,$records_per_page);
        foreach ($list as $key=>$val){
            unset($list[$key]['user_id']);
            $list[$key]['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
            $list[$key]['finance'] = number_format($val['finance']);
        }
        $result = array(
            "reqStatus" => true,
            "data" => array(
                "totalCount" => $totalCount ? $totalCount : 0,
                "pageIndex" => $pageIndex,
                "list" => $list
            )
        );
        echo JSON($result);
    }
    //积分明细
    function detail_experience(){
        $type = isset($_REQUEST['type']) ? trim($_REQUEST['type']) : 0;
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $start_date = isset($_REQUEST['start_date']) ? trim($_REQUEST['start_date']) : "";
        $end_date = isset($_REQUEST['end_date']) ? trim($_REQUEST['end_date']) : date("Y-m-d");
        $where = array();
        $where['user_id'] = $this->userinfo['user_id'];
        $type ? $where['type'] = $type : "";
        if($start_date){$where['create_time >='] = strtotime($start_date." 00:00:00");}
        if($end_date){$where['create_time <='] = strtotime($end_date." 23:59:59");} 
        $totalCount = $this->experience_list_model->get_count($where);
        
        $records_per_page = 5;
        $list = $this->experience_list_model->get_list($where,$pageIndex * $records_per_page,$records_per_page);
        foreach ($list as $key=>$val){
            unset($list[$key]['user_id']);
            $list[$key]['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
            $list[$key]['experience'] = number_format($val['experience']);
        }
        $result = array(
            "reqStatus" => true,
            "data" => array(
                "totalCount" => $totalCount ? $totalCount : 0,
                "pageIndex" => $pageIndex,
                "list" => $list
            )
        );
        echo JSON($result);
    }
    
    function trade_records(){
        $this->assign("end_time",  date("Y-m-d"));
        $this->display('user/trade_records.html');
    }
    //提现记录
    function cash_list(){
        $status = isset($_REQUEST['status']) ? trim($_REQUEST['status']) : 0;
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $start_date = isset($_REQUEST['start_date']) ? trim($_REQUEST['start_date']) : "";
        $end_date = isset($_REQUEST['end_date']) ? trim($_REQUEST['end_date']) : date("Y-m-d");
        $where = array();
        $where['user_id'] = $this->userinfo['user_id'];
        $status ? $where['status'] = $status : "";
        if($start_date){$where['create_time >='] = strtotime($start_date." 00:00:00");}
        if($end_date){$where['create_time <='] = strtotime($end_date." 23:59:59");} 
        $totalCount = $this->withdraw_order_model->getCount($where);
        
        $records_per_page = 5;
        $list = $this->withdraw_order_model->getList($where,$pageIndex * $records_per_page,$records_per_page);
        foreach ($list as $key=>$val){
            unset($list[$key]['user_id']);
            unset($list[$key]['order_nid']);
            $list[$key]['create_time'] = date("Y-m-d",$val['create_time']);
            $list[$key]['true_amount'] = sprintf("%0.2f",$val['true_amount']);
            if($val['without_type'] == 1){
               $list[$key]['finance'] = number_format($val['amount']*10000)."溜溜币";
            }else{
                $list[$key]['finance'] = intval($val['amount'])."溜珠";
            }
            $list[$key]['fee'] = sprintf("%0.2f",$val['fee']);
            $list[$key]['type'] = $val["type"] == 1 ? "支付宝" : "银行";
        }
        $result = array(
            "reqStatus" => true,
            "data" => array(
                "totalCount" => $totalCount ? $totalCount : 0,
                "pageIndex" => $pageIndex,
                "list" => $list
            )
        );
        echo JSON($result);
    }
    //商品兑换记录
    function exchange_list(){
        $status = isset($_REQUEST['status']) ? trim($_REQUEST['status']) : 0;
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $start_date = isset($_REQUEST['start_date']) ? trim($_REQUEST['start_date']) : "";
        $end_date = isset($_REQUEST['end_date']) ? trim($_REQUEST['end_date']) : date("Y-m-d");
        $where = array();
        $where['user_id'] = $this->userinfo['user_id'];
        $status ? $where['status'] = $status : "";
        if($start_date){$where['create_time >='] = strtotime($start_date." 00:00:00");}
        if($end_date){$where['create_time <='] = strtotime($end_date." 23:59:59");} 
        $totalCount = $this->exchange_order_model->getCount($where);
        
        $records_per_page = 5;
        $list = $this->exchange_order_model->getList($where,$pageIndex * $records_per_page,$records_per_page);
        foreach ($list as $key=>$val){
            unset($list[$key]['user_id']);
            unset($list[$key]['order_nid']);
            $list[$key]['create_time'] = date("Y-m-d",$val['create_time']);
            $list[$key]['amount'] = number_format($val['amount']);
        }
        $result = array(
            "reqStatus" => true,
            "data" => array(
                "totalCount" => $totalCount ? $totalCount : 0,
                "pageIndex" => $pageIndex,
                "list" => $list
            )
        );
        echo JSON($result);
    }
    
    function exper_list(){
        $where = array();
        $where['user_id'] = $this->userinfo['user_id'];//
        $total_awards = $this->plat_award_list_model->getTotal($where);
        $this->assign("total_awards", sprintf("%0.2f",$total_awards['award']/10000));
        $this->display('user/exper_list.html');
    }
    //加息明细
    function reward_details(){
        $records_per_page = 10;
        $where = "t.user_id = ".$this->userinfo['user_id'];
        $getCount = $this->db->query("select t.* from tan_plat_invest_task t where $where order by t.id desc")->num_rows();
        $page = new Paging($getCount,$records_per_page);
        $query_params = "t.id,t.title,t.plat_en,t.total_amount,t.amount,t.total_interest_finance,t.add_interest,t.extra_interest,t.status,t.invest_time,p.name_ch";
        $list = $this->db->query("select $query_params from tan_plat_invest_task t left join tan_plat p on p.name_en = t.plat_en where $where order by id desc limit ".(($page->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach($list as $key => $val){
            $list[$key]['id'] = encrypt_interests_id($val['id']);
            $list[$key]['invest_time'] = date("Y-m-d",$val['invest_time']);
            $list[$key]['total_interest_finance'] = number_format($val['total_interest_finance']);
            $list[$key]['add_interest'] = ($val['add_interest'] * 100)."%";
            $list[$key]['extra_interest'] = $val['extra_interest'] > 0 ? ($val['extra_interest'] * 100)."%" : 0;
            $list[$key]['detail_url'] = site_url("mplat_".$val['plat_en']);
        }
        $this->assign('list',$list);
        $this->assign('total',$getCount);
        $this->assign('showPage', $page->showpage());
        $this->display('user/reward_details.html');
    }
    
    function my_exper_list(){
        if(isset($_REQUEST['pageIndex'])){
            $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
            $records_per_page = 10;
            $where = "p.is_show = 1 and p.is_online = 1 and p.is_work = 1";
            $totalCount = $this->db->query("select i.* from tan_plat_info i left join tan_plat p on p.name_en = i.plat_en where $where order by p.sort desc,p.id desc")->num_rows();
            $list = $this->db->query("select i.plat_en name_en,c.* from tan_plat_info i left join tan_plat_ab_cpa c on c.plat_en = i.plat_en and c.user_id = ".$this->userinfo['user_id']." left join tan_plat p on p.name_en = i.plat_en where $where order by p.sort desc,p.id desc limit ".$pageIndex * $records_per_page.",".$records_per_page)->result_array();//$pageIndex * $records_per_page,$records_per_page
            foreach ($list as $key => $val){
                $list[$key]['reg_time'] = $val['reg_time'] ? date("Y-m-d",$val['reg_time']) : "--";
                $list[$key]['usernamep'] = $val['usernamep'] ? $val['usernamep'] : "--";
                $plat = $this->plat_model->get_plat(array("name_en"=>$val['name_en']));
                $list[$key]['is_online'] = $plat['is_show'];
                $list[$key]['is_flag'] = $val['usernamep'] ? 1 : 0;
                $list[$key]['title'] = $plat['name_ch'];
                $list[$key]['url'] = site_url("mplat_".$val['name_en']);
                $award_exper = $this->plat_award_list_model->get_award_exper_total(array('plat_en'=>$val['name_en'],"user_id"=>$val['user_id']));
                $list[$key]['award'] = $award_exper['award'] ? number_format($award_exper['award']) : "--";
                $list[$key]['exper'] = $award_exper['exper'] ? number_format($award_exper['exper']) : "--";
                $list[$key]['award_ids'] = $this->plat_award_list_model->get_no_recieve_list(array('plat_en'=>$val['name_en'],"user_id"=>$val['user_id'],"is_receive"=>0));
                unset($list[$key]['user_id']);
                unset($list[$key]['plat_id']);
            }
            $result = array(
                "reqStatus" => true,
                "sid" => $this->session_id,
                "data" => array(
                    "totalCount" => $totalCount ? $totalCount : 0,
                    "pageIndex" => $pageIndex,
                    "list" => $list,
                )
            );
            echo json_encode($result);exit;
        }
    }
    //推广好友页面
    function friends(){
        $where = array();
        $where['parent_id'] = $this->userinfo['user_id'];
        //总推广人数
        $total_count = $this->users_spread_model->get_user_total_count($where);
        $where = array();
        $where['reward_type'] = 'spread_award';//邀请类型奖励
        $where['user_id'] = $this->userinfo['user_id'];
        //总要求收益
        $total_finance =$this->finance_list_model->get_finance_total_count($where);
        //总要求积分
        $total_experience = $this->experience_list_model->get_experience_total_count($where);
        $this->assign('total_count', number_format($total_count));
        $this->assign('total_finance', number_format($total_finance));
        $this->assign('total_experience', number_format($total_experience));
        $this->assign("end_time",  date("Y-m-d"));
        $this->display('user/friends.html');
    }
    //推广好友记录
    function spread_friend(){
        $flag = isset($_REQUEST['flag']) ? $_REQUEST['flag'] : 0;
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $start_date = isset($_REQUEST['start_date']) ? trim($_REQUEST['start_date']) : "";
        $end_date = isset($_REQUEST['end_date']) ? trim($_REQUEST['end_date']) : date("Y-m-d");
        $records_per_page = 5;
        $where = array();
        if($flag){$flag == 1 ? $where['users.is_new'] = 1 : $where['users.is_new'] = 0 ;} 
        $start_date ? $where['users_spread.create_time >= '] = strtotime($start_date) : '';
        $end_date ? $where['users_spread.create_time <= '] = strtotime($end_date." 23:59:59") : '';
        $where['users_spread.parent_id'] = $this->userinfo['user_id'];
        $totalCount = $this->users_spread_model->get_count($where);
        $list = $this->users_spread_model->get_list($where,$pageIndex * $records_per_page,$records_per_page);
        foreach ($list as $key=>$val){
            $list[$key]['username'] = name_formart($val['username']);
            $where = array();
            $where['reward_type'] = 'spread_award';//邀请类型奖励
            $where['friend_id'] = $val['user_id'];
            $where['user_id'] = $this->userinfo['user_id'];
            $total_finance = $this->finance_list_model->get_finance_total_count($where);
            $total_experience = $this->experience_list_model->get_experience_total_count($where);
            $list[$key]['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
            $list[$key]['total_finance'] = number_format($total_finance);
            $list[$key]['total_experience'] = number_format($total_experience);
            $list[$key]['is_new'] = $val['is_new'] ? 1 : 0;
            unset($val['user_id']);
            unset($val['parent_id']);
        }
        
        $result = array(
            "reqStatus" => true,
            "data" => array(
                "totalCount" => $totalCount ? $totalCount : 0,
                "pageIndex" => $pageIndex,
                "list" => $list
            )
        );
        echo JSON($result);
    }
    //推广好友溜溜币
    function spread_finance(){
        $flag = isset($_REQUEST['flag']) ? $_REQUEST['flag'] : 0;
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $start_date = isset($_REQUEST['start_date']) ? trim($_REQUEST['start_date']) : "";
        $end_date = isset($_REQUEST['end_date']) ? trim($_REQUEST['end_date']) : date("Y-m-d");
        $records_per_page = 5;
        $where = array();
        $flag ? $where['friend_flag'] = $flag : "";
        if($start_date){$where['finance_list.create_time >='] = strtotime($start_date." 00:00:00");}
        if($end_date){$where['finance_list.create_time <='] = strtotime($end_date." 23:59:59");} 
        $where['reward_type'] = 'spread_award';
        $where['finance_list.user_id'] = $this->userinfo['user_id'];
        $totalCount = $this->finance_list_model->getCount($where);
        $list = $this->finance_list_model->getList($where,$pageIndex * $records_per_page,$records_per_page);
        foreach ($list as $key=>$val){
            $list[$key]['username'] = name_formart($val['username']);
            $list[$key]['finance'] = number_format($val['finance']);
            $list[$key]['friend_flag'] =  $this->friend_flag[$val['friend_flag']];
            $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
        }
        $result = array(
            "reqStatus" => true,
            "data" => array(
                "totalCount" => $totalCount ? $totalCount : 0,
                "pageIndex" => $pageIndex,
                "list" => $list
            )
        );
        echo JSON($result);
    }
    //推广好友积分
    function spread_experience(){
        $flag = isset($_REQUEST['flag']) ? $_REQUEST['flag'] : 0;
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $start_date = isset($_REQUEST['start_date']) ? trim($_REQUEST['start_date']) : "";
        $end_date = isset($_REQUEST['end_date']) ? trim($_REQUEST['end_date']) : date("Y-m-d");
        $records_per_page = 5;
        $where = array();
        $flag ? $where['friend_flag'] = $flag : "";
        if($start_date){$where['experience_list.create_time >='] = strtotime($start_date." 00:00:00");}
        if($end_date){$where['experience_list.create_time <='] = strtotime($end_date." 23:59:59");} 
        $where['reward_type'] = 'spread_award';
        $where['experience_list.user_id'] = $this->userinfo['user_id'];
        $totalCount = $this->experience_list_model->getCount($where);
        $list = $this->experience_list_model->getList($where,$pageIndex * $records_per_page,$records_per_page);
        foreach ($list as $key=>$val){
            $list[$key]['username'] = name_formart($val['username']);
            $list[$key]['experience'] = number_format($val['experience']);
            $list[$key]['friend_flag'] =  $this->friend_flag[$val['friend_flag']];
            $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
        }
        $result = array(
            "reqStatus" => true,
            "data" => array(
                "totalCount" => $totalCount ? $totalCount : 0,
                "pageIndex" => $pageIndex,
                "list" => $list
            )
        );
        echo JSON($result);
    }
    
    function cash(){
        $this->session->set_userdata("sms_code",null);
        //账号余额
        $balance = floor($this->user_info['balance']);
        $balance = intval($balance/10000);
        $withdraw_min_alipay = $this->config_award['withdraw_min_alipay'];//支付宝提现最低额度
        $withdraw_min_bank = $this->config_award['withdraw_min_bank'];//银行卡提现最低额度
        
        $where = array();
        $where['user_id'] = $this->userinfo['user_id'];
        $where['type'] = 1;//支付宝
        $alipay = $this->users_bank_model->get_list($where,0,2);
        
        $where['type'] = 2;
        $bank = $this->users_bank_model->get_list($where,0,2);
        $this->assign('withdraw_min_alipay', $withdraw_min_alipay);
        $this->assign('withdraw_min_bank', $withdraw_min_bank);
        $this->assign('balance', $balance);
        $this->assign("alipay_flag",$balance > $withdraw_min_alipay ? 1 : 0 );
        $this->assign("bank_flag",$balance > $withdraw_min_bank ? 1 : 0 );
        
        
        //社区溜珠
        $liuzhu_balance = $this->user_info['bbs_account']['balance'];
        $this->assign("liuzhu_alipay_flag",$liuzhu_balance > $withdraw_min_alipay ? 1 : 0 );
        $this->assign("liuzhu_bank_flag",$liuzhu_balance > $withdraw_min_bank ? 1 : 0 );
        $this->assign('liuzhu_balance', $liuzhu_balance);
        
        $this->assign("alipays", $alipay);
        $this->assign("banks", $bank);
        
        //VIP
        $next_vip = $this->config_vip[$this->user_info['vip_degree']+1];
        $this->assign('next_vip', $next_vip);
        
        $this->display('user/cash.html');
    }
    
    function do_vip(){
        if($_POST){
            $exper_balance = $this->user_info['experience_balance'];//用户当前可用积分
            $next_vip = $this->config_vip[$this->user_info['vip_degree']+1];
            $experience = $exper_balance - $next_vip['amount'];
            if(!$experience){
                if($experience != 0){
                    $mess['errorMsg'] = "您还未达到升级<span style='color:#F4772B;font-size:16px;'>VIP".$next_vip['degree']."</span>的条件哦";
                    $mess['errorCode'] = 1000;
                    $mess['reqStatus'] = true;
                    echo JSON($mess);exit;
                }
            }
            $next_degree = $this->user_info['vip_degree'] +1;
            $data['vip_degree'] = $next_degree;
            $result = $this->users_model->user_update($data,$this->user_info['user_id']);//升级成功
            if($result){
                //获取账户信息
                $account = $this->account_model->get($this->userinfo['user_id']);
                $account_data = array();
                $account_data['experience_balance'] = $account['experience_balance'] -  $next_vip['amount'];//可用积分值
                $account_data['experience_expend'] = $account['experience_expend'] + $next_vip['amount'];//可用积分值
                //更新账户
                $this->account_model->update($account_data,$this->userinfo['user_id']);
                
                //写入积分明细
                $experience_list_data = array();
                $experience_list_data['user_id'] = $this->userinfo['user_id'];
                $experience_list_data['content'] = '升级会员VIP'.$next_vip['degree'].'，消耗'.$next_vip['amount'].'积分';
                $experience_list_data['experience'] = $next_vip['amount'];
                $experience_list_data['experience_balance'] = $account_data['experience_balance'];
                $experience_list_data['reward_type'] = 'exchange_deduct';
                $experience_list_data['type'] = 2;
                $experience_list_data['create_time'] = time();
                $experience_list_data['month'] = date("Ym");
                $experience_list_data['day'] = date("Ymd");
                $this->experience_list_model->insert($experience_list_data);
                //日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'vip';
                $log_data['content'] = '会员升级';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $sign_s = $this->config_award['mark_award'][$next_vip['degree']];//签到奖励
                $exper_s = $this->config_award['exper_award'][$next_vip['degree']];//体验奖励
                $mess['reqStatus'] = true;
                $mess['errorCode'] = 1001;
                $mess['sign_award'] = ($sign_s * 100)."%";
                $mess['exper_award'] = ($exper_s * 100)."%";
                echo JSON($mess);exit;
                
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1004;
                $mess['errorMsg'] = "升级失败";
                echo JSON($mess);exit; 
            }
        }
    }
    
    function do_cash(){
        //账号余额
        $balance = floor($this->user_info['balance']);
        $balance = intval($balance/10000);
        $withdraw_min_alipay = $this->config_award['withdraw_min_alipay'];//支付宝提现最低额度
        $withdraw_min_bank = $this->config_award['withdraw_min_bank'];//银行卡提现最低额度
        if($_POST){
            $mess = array();
            $type = trim($_POST['type']);//提现方式
            $accountId = trim($_POST['accountId']);//提现账户
            $payPass = trim($_POST['payPass']);//交易密码
            $cashValue = trim($_POST['cashValue']);//提现金额
            
            $SSID = isset($_POST['keyCode']) ? trim($_POST['keyCode']) : "";
            if($SSID != $this->session_id){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            
            
            if(!$type && $type != 1 && $type != 2){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "请选择提现方式";
                echo JSON($mess);exit;
            }
            $bank = $this->users_bank_model->get(array('user_id'=>$this->userinfo['user_id'],'id'=>$accountId));
            if(!$bank){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "请选择提现账号";
                echo JSON($mess);exit;
            }
            //是否大于余额
            if($cashValue > $balance){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "溜溜币不足，无法提现哦";
                echo JSON($mess);exit;
            }
            if($type == 1 && $cashValue < $withdraw_min_alipay){//支付宝
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "支付宝最低提现<i style='color:red;'>$withdraw_min_alipay</i>元";
                echo JSON($mess);exit;
            }
            
            if($type == 2 && $cashValue < $withdraw_min_bank){//银行卡
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "银行卡最低提现<i style='color:red;'>$withdraw_min_bank</i>元";
                echo JSON($mess);exit;
            }
            //提现金额小于20，扣除0.5手续费
            if($cashValue < 20){
                $fee = $this->config_award['withdraw_fee'];
            }else{
                $fee = 0.0;
            }
            $amount = intval($cashValue)*10000;
            $type_tile = $type == 1 ? "支付宝" : "银行卡";
            //检测交易密码
            
            if($this->user_info['paypassword'] != md5(md5($payPass) .$this->user_info['salt'])){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "请输入正确的交易密码";
                echo JSON($mess);exit;
            }
            //处理提现订单MakeOrdId
            $cash_order = array();
            $cash_order['user_id'] = $this->userinfo['user_id'];
            $cash_order['order_nid'] = MakeOrdId($this->userinfo['user_id']);
            $cash_order['amount'] = $cashValue;
            $cash_order['fee'] = $fee;
            $cash_order['true_amount'] = $cashValue - $fee;
            $cash_order['account'] = $bank['account'];
            $cash_order['name'] = $bank['name'];
            $cash_order['bank_type'] = $bank['bank_type'];
            $cash_order['bank_home'] = $bank['bank_home'];
            $cash_order['type'] = $type;
            $cash_order['status'] = 2;
            $cash_order['create_time'] = time();
            $result = $this->withdraw_order_model->insert($cash_order);
            if($result){//申请成功
                //获取用户账户信息
                $account = $this->account_model->get($this->userinfo['user_id']);
                $account_data = array();
                $account_data['balance'] = $account['balance'] - $amount;//账户余额
                $account_data['expend'] = $account['expend'] + $amount;//总支出
                //更新账户
                $this->account_model->update($account_data,$this->userinfo['user_id']);
                //生成提现记录
                $withdraw_data = array();
                $withdraw_data['user_id'] = $cash_order['user_id'];
                $withdraw_data['order_nid'] = $cash_order['order_nid'];
                $withdraw_data['amount'] = $cash_order['amount'];
                $withdraw_data['fee'] = $cash_order['fee'];
                $withdraw_data['finance'] = $amount;
                $withdraw_data['status'] = $cash_order['status'];
                $withdraw_data['create_time'] = time();
                $withdraw_data['account'] = $cash_order['account'];
                $withdraw_data['truename'] = $cash_order['name'];
                $this->withdraw_list_model->insert($withdraw_data);
                //溜溜币明细
                $finance_list_data = array();
                $finance_list_data['user_id'] = $this->userinfo['user_id'];
                $finance_list_data['content'] = $type_tile."提现".$cashValue."元";
                $finance_list_data['finance'] = $amount;
                $finance_list_data['finance_balance'] = $account_data['balance'];
                $finance_list_data['reward_type'] = 'withdraw_deduct';
                $finance_list_data['type'] = 2;
                $finance_list_data['create_time'] = time();
                $finance_list_data['month'] = date("Ym");
                $finance_list_data['day'] = date("Ymd");
                $this->finance_list_model->insert($finance_list_data);
                
                //日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'withdraw';
                $log_data['content'] = '申请提现';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $mess['reqStatus'] = true;
                $mess['errorCode'] = 0;
                echo JSON($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "提现失败";
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    //溜珠提现
    function do_lzcash(){
        //账号余额
        $balance = floor($this->user_info['bbs_account']['balance']);
        $withdraw_min_alipay = $this->config_award['withdraw_min_alipay'];//支付宝提现最低额度
        $withdraw_min_bank = $this->config_award['withdraw_min_bank'];//银行卡提现最低额度
        if($_POST){
            $mess = array();
            $type = trim($_POST['type']);//提现方式
            $accountId = trim($_POST['accountId']);//提现账户
            $payPass = trim($_POST['payPass']);//交易密码
            $cashValue = trim($_POST['cashValue']);//提现金额
            
            $SSID = isset($_POST['keyCode']) ? trim($_POST['keyCode']) : "";
            if($SSID != $this->session_id){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            
            if(!$type && $type != 1 && $type != 2){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "请选择提现方式";
                echo JSON($mess);exit;
            }
            $bank = $this->users_bank_model->get(array('user_id'=>$this->userinfo['user_id'],'id'=>$accountId));
            if(!$bank){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "请选择提现账号";
                echo JSON($mess);exit;
            }
            //是否大于余额
            if($cashValue > $balance){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "溜溜币不足，无法提现哦";
                echo JSON($mess);exit;
            }
            if($type == 1 && $cashValue < $withdraw_min_alipay){//支付宝
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "支付宝最低提现<i style='color:red;'>$withdraw_min_alipay</i>元";
                echo JSON($mess);exit;
            }
            
            if($type == 2 && $cashValue < $withdraw_min_bank){//银行卡
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "银行卡最低提现<i style='color:red;'>$withdraw_min_bank</i>元";
                echo JSON($mess);exit;
            }
            $amount = 0;
            //检测交易密码
            
            if($this->user_info['paypassword'] != md5(md5($payPass) .$this->user_info['salt'])){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "请输入正确的交易密码";
                echo JSON($mess);exit;
            }
            //处理提现订单MakeOrdId
            $cash_order = array();
            $cash_order['user_id'] = $this->userinfo['user_id'];
            $cash_order['order_nid'] = MakeOrdId($this->userinfo['user_id']);
            $cash_order['amount'] = $cashValue;
            $cash_order['fee'] = 0;
            $cash_order['true_amount'] = $cashValue;
            $cash_order['account'] = $bank['account'];
            $cash_order['name'] = $bank['name'];
            $cash_order['bank_type'] = $bank['bank_type'];
            $cash_order['bank_home'] = $bank['bank_home'];
            $cash_order['type'] = $type;
            $cash_order['without_type'] = 2;//溜珠提现
            $cash_order['status'] = 2;
            $cash_order['create_time'] = time();
            $result = $this->withdraw_order_model->insert($cash_order);
            if($result){//申请成功
                //获取用户账户信息
                $account = $this->db->query("select * from tan_bbs_account where user_id = ".$this->userinfo['user_id']." limit 1")->row_array();
                $account_data = array();
                $account_data['balance'] = $account['balance'] - intval($cashValue);//账户余额
                $account_data['expend'] = $account['expend'] + intval($cashValue);//总支出
                //更新账户
                $this->db->where(array("user_id"=>$this->userinfo['user_id']))->update("bbs_account",$account_data);
                //生成提现记录
                $withdraw_data = array();
                $withdraw_data['user_id'] = $cash_order['user_id'];
                $withdraw_data['order_nid'] = $cash_order['order_nid'];
                $withdraw_data['amount'] = $cash_order['amount'];
                $withdraw_data['fee'] = $cash_order['fee'];
                $withdraw_data['finance'] = $amount;
                $withdraw_data['status'] = $cash_order['status'];
                $withdraw_data['create_time'] = time();
                $withdraw_data['account'] = $cash_order['account'];
                $withdraw_data['truename'] = $cash_order['name'];
                $this->withdraw_list_model->insert($withdraw_data);
                
                //溜珠明细
                $this->db->insert("bbs_zhu_detail",array("user_id"=>$this->userinfo['user_id'],"num"=>intval($cashValue),"type"=>"提现","style"=>2,"create_time"=> time()));
                //日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'withdraw';
                $log_data['content'] = '溜珠申请提现';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $mess['reqStatus'] = true;
                $mess['errorCode'] = 0;
                echo JSON($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "提现失败";
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    function sms(){
        $this->display('user/sms.html');
    }
    
    function my_sms(){
        $where = array();
        $where['user_id'] = $this->userinfo['user_id'];
        $where['is_del'] = 0;
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $records_per_page = 6;
        $totalCount = $this->users_award_notice_model->get_count($where);
        $list = $this->users_award_notice_model->get_list($where,$pageIndex * $records_per_page,$records_per_page);
        $str = "";
        foreach ($list as $val){
            $str .= "<li>
                    <div class='txt left'>
                        <b class='is-see-status left icon ".($val['is_see'] ? "icon-read" : "icon-unread")."'></b>
                        <div class='txt-title left'>
                            <h4>".$val['title']."</h4>
                            <div class='content'>".  strip_tags($val['content'])."</div>
                        </div>
                    </div>
                    <div class='state right'>
                        <i class='is-see'>".($val['is_see'] ? "已读" : "未读")."</i>
                        <b>".  date("Y-m-d H:i:s",$val['create_time'])."</b>
                        <input name='sms' class='sms' type='checkbox' data-id='".$val['id']."'/>
                    </div>
                    <div class='clear'></div>
                    <span class='InfBtn' data-status='0'><p type='1' data-id='".$val['id']."' data-status='".$val['is_see']."'>展开查看信息</p></span>
                </li>";
        }
        $str .= "<div class='clear'></div>";
        $result = array(
            "reqStatus" => true,
            "data" => array(
                "totalCount" => $totalCount ? $totalCount : 0,
                "pageIndex" => $pageIndex,
                "data" => $str
            )
        );
        echo json_encode($result,JSON_HEX_TAG);die;
    }
    
    function notice_sms(){
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $where = array();
        $where['code'] = 'report';
        $cate = $this->cate_model->content_cate_get($where);
        $records_per_page = 6;
        $where = array();
        $where['cate_id'] = $cate['id'];
        $where['status'] = 1;
        $totalCount = $this->content_model->get_count($where);
        $list = $this->content_model->get_report_list($where,$pageIndex * $records_per_page,$records_per_page);
        $str = "";
        foreach ($list as $val){
            $val['content'] = strstr($val['content'],"http://") ?  : str_replace('<img src="', '<img src="http://www.tan66.com', $val['content']);
            $url = site_url('news/'.$cate['id'].'/'.$val['id']);
            $str .= "<li>
                        <div class='txt left'>
                            <b class='is-see-status left icon ".($val['is_see'] ? "icon-read" : "icon-unread")."'></b>
                            <div class='txt-title left'>
                                    <a href='".$url."' target='_blank'><h4>".$val['title']."</h4></a>
                                    <div class='content'>".$val['content']."</div>
                            </div>
                        </div>
                        <div class='state right'>
                            <i class='is-see'>".($val['is_see'] ? "已读" : "未读")."</i>
                            <b>".  date("Y-m-d H:i:s",$val['create_time'])."</b>
                            <input class='notice' type='checkbox' data-id='".$val['id']."'/>
                        </div>
                        <div class='clear'></div>
                        <span class='InfBtn' data-status='0'><p type='2' data-id='".$val['id']."' data-status='".($val['is_see'] ? 1 : 0)."'>展开查看信息</p></span>
                    </li>";
        }
        $result = array(
            "reqStatus" => true,
            "data" => array(
                "totalCount" => $totalCount ? $totalCount : 0,
                "pageIndex" => $pageIndex,
                "data" => $str
            )
        );
        echo json_encode($result,JSON_HEX_TAG);die;
    }
    function set_sms_status(){
        $sms_ids = trim($_POST['ids']);
        $sms_ids = explode(",", $sms_ids);
        $data = array();
        $data['is_see'] = 1;
        $this->users_award_notice_model->update_all($data,$sms_ids);
        $mess['errorMsg'] = '设置成功';
        $mess['reqStatus'] = true;
        echo JSON($mess);exit;
    }
    
    function set_notice_status(){
        $sms_ids = trim($_POST['ids']);
        $sms_ids = explode(",", $sms_ids);
        $data = array();
        foreach($sms_ids as $id){
            $temp = array();
            $temp['user_id'] = $this->userinfo['user_id'];
            $temp['report_id'] = $id;
            $temp['is_see'] = 1;
            $temp['create_time'] = time();
            $data[] = $temp;
        }
        $this->content_model->insert_all($data);
        $mess['errorMsg'] = '设置成功';
        $mess['reqStatus'] = true;
        echo JSON($mess);exit;
    }
    
    function set_read(){
        $ids = trim($_POST['ids']);
        $type = trim($_POST['type']);
        if($type == "sms"){
            $sms_ids = explode(",", $ids);
            $data = array();
            $data['is_see'] = 1;
            $this->users_award_notice_model->update_all($data,$sms_ids);
        }else{
            $sms_ids = explode(",", $ids);
            $data = array();
            foreach($sms_ids as $id){
                $temp = array();
                $temp['user_id'] = $this->userinfo['user_id'];
                $temp['report_id'] = $id;
                $temp['is_see'] = 1;
                $temp['create_time'] = time();
                $data[] = $temp;
            }
            $this->content_model->insert_all($data);
        }
        $mess['errorMsg'] = '设置成功';
        $mess['reqStatus'] = true;
        echo JSON($mess);exit;
    }
    function set_sms_del(){
        $sms_ids = trim($_POST['ids']);
        $sms_ids = explode(",", $sms_ids);
        $data = array();
        $data['is_del'] = 1;
        $this->users_award_notice_model->update_all($data,$sms_ids);
        $mess['errorMsg'] = '设置成功';
        $mess['reqStatus'] = true;
        echo JSON($mess);exit;
    }
    function security(){
        $this->session->set_userdata("sms_code",null);
        $mobile = $this->user_info['mobile'];
        $email = $this->user_info['email'];
        $payPass = $this->user_info['paypassword'];
        
        $where = array();
        $where['user_id'] = $this->user_info['user_id'];
        //获取绑定的qq
        $qq_bind = $this->users_model->get_qq_bind($where);
        if($qq_bind){
            $qq_bind['nickname'] = $qq_bind['nickname'] ? name_formart($qq_bind['nickname']) : '';
        }
        //获取绑定的微博
        $weibo_bind = $this->users_model->get_weibo_bind($where);
        if($weibo_bind){
            $weibo_bind['nickname'] = $weibo_bind['nickname'] ? name_formart($weibo_bind['nickname']) : '';
        }
        
        //收货地址
        $address_list = $this->users_model->get_shipping_address_list(array("user_id"=>$this->user_info['user_id'],'is_show'=>1));
        foreach($address_list as $key=>$val){
            $address_list[$key]['mobile'] = mobile_formart($val['mobile']);
        }
        $this->assign('qq_bind',$qq_bind);
        $this->assign('address_count',  count($address_list));
        $this->assign('address_list',$address_list);
        $this->assign('weibo_bind',$weibo_bind);
        $this->assign("mobile",$mobile ? mobile_formart($mobile) : "");
        $this->assign("email",$email ? email_formart($email) : "");
        $this->assign("paypass", $payPass ? 1 : 0);
        $this->display('user/security.html');
    }
    //绑定手机
    function bind_mobile(){
        if($_POST){
            $mobile = isset($_POST['uMobile']) ? trim($_POST['uMobile']) : "";
             //获取用户信息
            $user = $this->users_model->get_user_by_uid($this->userinfo['user_id']);
            $data = array();
            $data['mobile'] = $mobile;
            $user['mobile_isApprove'] == 0 ? $data['mobile_isApprove'] = 1 : "";
            $result = $this->users_model->user_update($data,  $this->userinfo['user_id']);
            if($result){
                $this->users_model->users_alter($this->userinfo['user_id'],"isupdate");
                if($user['mobile_isApprove'] == 0 ){
                    
                    /*******************/
                    //判断是否线下注册,如果是，则删除
                    $line_user = $this->users_model->get_user(array("mobile"=>$data['mobile'],"is_line"=>1));
                    if($line_user){
                        $this->db->where(array('user_id'=>$line_user['user_id']))->delete("tan_usres");
                        $this->db->where(array('user_id'=>$line_user['user_id']))->delete("tan_usres_info");
                        $this->db->where(array('mobile'=>$mobile))->update("tan_users_line",array('is_reg'=>1));
                        //转移投资记录
                        $this->db->where(array('user_id'=>$line_user['user_id']))->update("tan_plat_invest_list",array("user_id"=>$this->userinfo['user_id']));
                    }
                    /***********************/
                    
                    $this->new_task = $this->config_task['new_task'];
                    $finance_award = $this->new_task[1]['award_finance'];
                    $experience_award = $this->new_task[1]['award_experience'];
                    //获取账号信息
                    $account = $this->account_model->get($this->userinfo['user_id']);
                    //更新账号信息
                    $account_data = array();
                    $account_data['total'] = $account['total'] + $finance_award;
                    $account_data['balance'] = $account['balance'] + $finance_award;
//                    $account_data['experience'] = $account['experience'] + $experience_award;
//                    $account_data['experience_balance'] = $account['experience_balance'] + $experience_award;
                    $this->account_model->update($account_data,$this->userinfo['user_id']);
                    //奖励溜溜币
                    $finance_data = array();
                    $finance_data['user_id'] = $this->userinfo['user_id'];
                    $finance_data['content'] = "首次绑定手机奖励".$finance_award."溜溜币";
                    $finance_data['finance'] = $finance_award;
                    $finance_data['finance_balance'] = $account_data['balance'];
                    $finance_data['reward_type'] = 'task_award';
                    $finance_data['type'] = 1;
                    $finance_data['create_time'] = time();
                    $finance_data['month'] = date("Ym");
                    $finance_data['day'] = date("Ymd");
                    $this->finance_list_model->insert($finance_data);
                    //奖励积分
//                    $experience_data = array();
//                    $experience_data['user_id'] = $this->userinfo['user_id'];
//                    $experience_data['content'] = "首次绑定手机奖励".$experience_award."积分";
//                    $experience_data['experience'] = $experience_award;
//                    $experience_data['experience_balance'] = $account_data['experience'];
//                    $experience_data['reward_type'] = 'task_award';
//                    $experience_data['type'] = 1;
//                    $experience_data['create_time'] = time();
//                    $experience_data['month'] = date("Ym");
//                    $experience_data['day'] = date("Ymd");
//                    $this->experience_list_model->insert($experience_data);
                    
                    $new_task_step = array();
                    $new_task_step['is_finish'] = 1;
                    $new_task_step['is_receive'] = 1;
                    $new_task_step['finish_time'] = time();
                    $new_task_step['receive_time'] = time();
                    $this->new_task_model->update($new_task_step,array('user_id'=>$this->userinfo['user_id'],'step'=>2));
                    
                    //奖励通知
                    $message = '恭喜您完成新手任务第二步【'.$this->new_task[1]['title'].'】，奖励您'.$finance_award."溜溜币。感谢您对弹溜溜的支持！";//和".$experience_award."积分
                    $this->db->insert("users_award_notice", array('user_id'=>$this->userinfo['user_id'],'title'=>'任务通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                    
                    //写入日志记录
                    $log_data = array();
                    $log_data['user_id'] = $this->userinfo['user_id'];
                    $log_data['events'] = 'newbindphone';
                    $log_data['content'] = '手机首绑成功';
                    $log_data['ip'] = $this->input->ip_address();
                    $log_data['create_time'] = time();
                    $this->users_log_model->insert($log_data);
                }
                
                //写入日志记录
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'bindphone';
                $log_data['content'] = '绑定成功'.$mobile;
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                
                $this->session->set_userdata("sms_code",null);
                $mess['errorMsg'] = '绑定成功';
                $mess['reqStatus'] = true;
                echo JSON($mess);exit;
            }else{
                $mess['errorMsg'] = '手机绑定失败，请重试';
                $mess['errorCode'] = 1003;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    //绑定邮箱
    function bind_email(){
        if($_POST){
            $email = isset($_POST['uEmail']) ? trim($_POST['uEmail']) : "";
             //获取用户信息
            $user = $this->users_model->get_user_by_uid($this->userinfo['user_id']);
            $data = array();
            $data['email'] = $email;
            $user['email_isApprove'] == 0 ? $data['email_isApprove'] = 1 : "";
            $result = $this->users_model->user_update($data,  $this->userinfo['user_id']);
            if($result){
                $this->users_model->users_alter($this->userinfo['user_id'],"isupdate");
                //写入日志记录
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'bindemail';
                $log_data['content'] = '绑定成功'.$email;
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                
                $this->session->set_userdata('email_code',"");
                $mess['errorMsg'] = '绑定成功';
                $mess['reqStatus'] = true;
                echo JSON($mess);exit;
            }else{
                $mess['errorMsg'] = '邮箱绑定失败，请重试';
                $mess['errorCode'] = 1003;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    
    //修改交易密码
    function set_paypass(){
        if($_POST){
            
            $payPass = isset($_POST['payPass']) ? trim($_POST['payPass']) : "";//新交易密码
            $smsCode = isset($_POST['smsCode']) ? trim($_POST['smsCode']) : "";//短信验证码
            if(!$payPass){
                $mess['errorMsg'] = '参数传输有误';
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            if(!$smsCode){
                $mess['errorMsg'] = '参数传输有误';
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            // 验证短信号码
            if($smsCode != $this->session->userdata('sms_code')){
                $mess['errorMsg'] = '参数传输有误';
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            $md5paypass = md5(md5($payPass) .$this->user_info['salt']);
            $data['paypassword'] = $md5paypass;
            $result = $this->users_model->user_update($data,  $this->userinfo['user_id']);
            if($result){
                //写入日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'modify';
                $log_data['content'] =  $this->user_info['paypassword'] ? '交易密码设置成功' : '交易密码修改成功';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $this->session->set_userdata("sms_code",null);
                $mess['errorMsg'] = $this->user_info['paypassword'] ? '交易密码设置成功' : '交易密码修改成功';
                $mess['reqStatus'] = true;
                echo JSON($mess);exit;
                
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1003;
                $mess['errorMsg'] = "设置失败，请重新操作";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    //设置登录密码
    function set_loginpass(){
        if($_POST){
            $upass = isset($_POST['loginPass']) ? trim($_POST['loginPass']) : "";//原密码
            $newpass = isset($_POST['newLoginPass']) ? trim($_POST['newLoginPass']) : "";//新密码
            if(!$upass){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "原登录密码不能为空";
                echo JSON($mess);exit;
            }
            if(!$newpass){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "新登录密码不能为空";
                echo JSON($mess);exit;
            }
            //验证原始密码是否正确
            if($this->user_info['password'] != md5(md5($upass) .$this->user_info['salt'])){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "原登录密码错误";
                echo JSON($mess);exit;
            }
            //新加密登录密码
            $md5pass =  md5(md5($newpass) .$this->user_info['salt']);
            $data['password'] = $md5pass;
            $result = $this->users_model->user_update($data,  $this->userinfo['user_id']);
            if($result){
                $this->users_model->users_alter($this->userinfo['user_id'],"isupdatepw");
                //写入日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'modify';
                $log_data['content'] = '登录密码修改成功';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "修改成功";
                echo JSON($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1003;
                $mess['errorMsg'] = "设置失败，请重新操作";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    //设置基本信息
    function set_bascinfo(){
        if($_POST){
            $truename = isset($_POST['trueName']) ? trim($_POST['trueName']) : "";
            $nickname = isset($_POST['nickName']) ? trim($_POST['nickName']) : "";
            $sex = isset($_POST['sex']) ? trim($_POST['sex']) : 1;
            $qq = isset($_POST['QQ']) ? trim($_POST['QQ']) : "";
            $signature = isset($_POST['sigNature']) ? trim($_POST['sigNature']) : "";
            $data_info = array();
            $data_info['nickname'] = $nickname;
            $data_info['truename'] = $truename;
            $data_info['sex'] = $sex;
            $data_info['qq'] = $qq;
            $data_info['signature'] = $signature;
            $result = $this->users_model->user_info_update($data_info,  $this->userinfo['user_id']);
            $this->users_model->users_alter($this->userinfo['user_id'],"isupdate");
            if($result){
                //写入日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'modify';
                $log_data['content'] = '修改基本信息';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "修改成功";
                echo JSON($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1003;
                $mess['errorMsg'] = "设置失败，请重新操作";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    
    //添加收货地址
    function set_address(){
        if($_POST){
            $name = isset($_POST['receiverName']) ? trim($_POST['receiverName']) : "";
            $mobile = isset($_POST['receiverMobile']) ? trim($_POST['receiverMobile']) : "";
            $province = isset($_POST['receiverPrivince']) ? trim($_POST['receiverPrivince']) : "";
            $city = isset($_POST['receiverCity']) ? trim($_POST['receiverCity']) : "";
            $address = isset($_POST['receiverAddress']) ? trim($_POST['receiverAddress']) : "";
            
            $flag = $this->users_model->get_shipping_address_count(array("user_id"=>$this->userinfo['user_id'],'is_show'=>1,'is_default'=>1));
            
            $data_info = array();
            $data_info['user_id'] = $this->userinfo['user_id'];
            $data_info['name'] = $name;
            $data_info['mobile'] = $mobile;
            $data_info['address'] = $province."&nbsp;".$city."&nbsp;".$address;
            $data_info['is_show'] = 1;
            $data_info['is_default'] = $flag ? 0 : 1;
            $data_info['create_time'] = time();
            $result = $this->users_model->insert_address($data_info);
            if($result){
                //写入日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'add_address';
                $log_data['content'] = '添加收货地址';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "添加成功";
                echo JSON($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1003;
                $mess['errorMsg'] = "设置失败，请重新操作";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    //设置收货地址的删除状态信息
    function set_address_del(){
        if($_POST){
            $id = isset($_POST['id']) ? trim($_POST['id']) : "";
            $flag = $this->users_model->get_shipping_address_count(array("id"=>$id,'is_show'=>1));
            if(!$flag){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "未找到该条记录";
                echo JSON($mess);exit;
            }
            $data_info = array();
            $data_info["is_show"] = 0;
            $result = $this->users_model->update_address($data_info,$id);
            if($result){
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "设置成功";
                echo JSON($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "设置失败，请重新操作";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    
    //设置收货地址的默认状态信息
    function set_address_default(){
        if($_POST){
            $id = isset($_POST['id']) ? trim($_POST['id']) : "";
            $flag = $this->users_model->get_shipping_address_count(array("id"=>$id,'is_show'=>1));
            if(!$flag){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "未找到该条记录";
                echo JSON($mess);exit;
            }
            $default_address = $this->users_model->get_shipping_address_list(array("is_default"=>1,'user_id'=>$this->userinfo['user_id']));
            if($default_address){
                $this->users_model->update_address(array("is_default"=>0),$default_address[0]['id']);
            }
            $data_info = array();
            $data_info["is_default"] = 1;
            $result = $this->users_model->update_address($data_info,$id);
            if($result){
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "设置成功";
                echo JSON($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "设置失败，请重新操作";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    
    function cash_bind(){
        $this->session->set_userdata("sms_code",null);
        $where = array();
        $where['user_id'] = $this->userinfo['user_id'];
        $where['type'] = 1;//支付宝
        $alipay_list = $this->users_bank_model->get_list($where,0,2);
        
        $where['type'] = 2;//银行卡
        $bank_list = $this->users_bank_model->get_list($where,0,2);
        $this->assign("alipay_list", $alipay_list);
        $this->assign("bank_list", $bank_list);
        $this->display('user/cash_bind.html');
    }
    
    function bind_alipay(){
        if($_POST){
            $name = trim($_POST['Name']);
            $account = trim($_POST['Account']);
            $where['user_id'] = $this->userinfo['user_id'];
            $where['type'] = 1;//支付宝
            $alipay_list = $this->users_bank_model->get_list($where);
            if(count($alipay_list) >= 2){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "绑定账户数最多2个，您已达上限";
                echo JSON($mess);exit;
            }
            $data = array();
            $data['user_id'] = $this->userinfo['user_id'];
            $data['name'] = $name;
            $data['account'] = $account;
            $data['type'] = 1;
            $data['create_time'] = time();
            $result = $this->users_bank_model->insert($data);
            if($result){
                 //写入日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'binding';
                $log_data['content'] = '绑定支付宝成功'.$name.$account;
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $where = array();
                $where['user_id'] = $this->userinfo['user_id'];
                $where['type'] = 1;//支付宝
                $alipay_list = $this->users_bank_model->get_list($where);
                $str = "<tr><th>姓名</th><th>支付宝账号</th><th>操作</th></tr>";
                foreach ($alipay_list as $key=>$val){
                    $str .= "<tr><td>".$val['name']."</td><td>".$val['account']."</td><td class='delete-bind'><a href='javascript:void(0);' data-id='".$val['id']."'  data-type='1'>删除</a></td></tr>";
                }
                $this->session->set_userdata("sms_code",null);
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "绑定成功";
                $mess['data'] = $str;
                
                echo json_encode($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "绑定失败";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    
    function bind_bank(){
        if($_POST){
            $name = trim($_POST['Name']);
            $account = trim($_POST['Account']);
            $bank_type = trim($_POST['bankType']);
            $bank_home = trim($_POST['bankHome']);
            
            $where['user_id'] = $this->userinfo['user_id'];
            $where['type'] = 2;//银行卡
            $bank_list = $this->users_bank_model->get_list($where);
            if(count($bank_list) >= 2){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "绑定账户数最多2个，您已达上限";
                echo JSON($mess);exit;
            }
            
            $data = array();
            $data['user_id'] = $this->userinfo['user_id'];
            $data['name'] = $name;
            $data['account'] = $account;
            $data['bank_type'] = $bank_type;
            $data['bank_home'] = $bank_home;
            $data['type'] = 2;
            $data['create_time'] = time();
            $result = $this->users_bank_model->insert($data);
            if($result){
                //写入日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'binding';
                $log_data['content'] = '绑定银行卡成功'.$name.$account.$bank_home;
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $where = array();
                $where['user_id'] = $this->userinfo['user_id'];
                $where['type'] = 2;//银行卡
                $alipay_list = $this->users_bank_model->get_list($where);
                $str = "<tr><th>姓名</th><th>卡号</th><th>银行类型</th><th>操作</th></tr>";
                foreach ($alipay_list as $key=>$val){
                    $str .= "<tr><td>".$val['name']."</td><td>".$val['account']."</td><td>".$val['bank_type']."</td><td class='delete-bind'><a href='javascript:void(0);' data-id='".$val['id']."' data-type='1'>删除</a></td></tr>";
                }
                $this->session->set_userdata("sms_code",null);
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "绑定成功";
                $mess['data'] = $str;
                
                echo json_encode($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "绑定失败";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    //删除绑定提现记录
    function bind_del(){
        if($_POST){
            $type = trim($_POST['type']);
            $id = intval($_POST['id']);
            $where = array();
            $where['id'] = $id;
            $result = $this->users_bank_model->delete($where);
            if($result){
                $where = array();
                $where['user_id'] = $this->userinfo['user_id'];
                $where['type'] = $type;//银行卡
                $list = $this->users_bank_model->get_list($where);
                if($type == 1){
                    $str = "<tr><th>姓名</th><th>支付宝账号</th><th>操作</th></tr>";
                    foreach ($list as $key=>$val){
                        $str .= "<tr><td>".$val['name']."</td><td>".$val['account']."</td><td class='delete-bind'><a href='javascript:void(0);' data-id='".$val['id']."'  data-type='1'>删除</a></td></tr>";
                    }
                }else{
                    $str = "<tr><th>姓名</th><th>卡号</th><th>银行类型</th><th>操作</th></tr>";
                    foreach ($list as $key=>$val){
                        $str .= "<tr><td>".$val['name']."</td><td>".$val['account']."</td><td>".$val['bank_type']."</td><td class='delete-bind'><a href='javascript:void(0);' data-id='".$val['id']."' data-type='1'>删除</a></td></tr>";
                    }
                }
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "已删除";
                $mess['data'] = $str;
                
                echo json_encode($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "操作失败";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    function sign(){
        //加载日历类
        $this->load->library('MCalendar');
        $MCalendar = new MCalendar();
        $calendar = $MCalendar->display();
        $this_year = date("Y");
        $this_month = date("m");
        //获取上个月签到的天数
        $where = array();
        $where['year_time'] = $this_month == "01" ? $this_year-1 : $this_year;
        $where['month_time'] = $this_month == "01" ? "12" : $this_month-1;
        $where['user_id'] = $this->userinfo['user_id'];
        $pre_signs = $this->users_model->get_user_sign($where);
        $pre_sign_days = "";
        if($pre_signs){
            foreach ($pre_signs as $key=>$val){
                $pre_sign_days .= $val['day_time'].",";
            }
        }
        //获取当前月签到的天数
        $where = array();
        $where['year_time'] = $this_year;
        $where['month_time'] = $this_month;
        $where['user_id'] = $this->userinfo['user_id'];
        $current_signs = $this->users_model->get_user_sign($where);
        $current_sign_days = "";
        if($current_signs){
            foreach ($current_signs as $key=>$val){
                $current_sign_days .= $val['day_time'].",";
            }
        }
        //获取今日签到数据
        $sign_today = $this->users_model->get_user_sign(array("user_id"=>$this->userinfo['user_id'],"year_time"=>date("Y"),"month_time"=>date("m"),"day_time"=>date("d")));
        $sign_today_data = $sign_today ? $sign_today[0] : "";
        $this->assign("sign_today_data", $sign_today_data);
        $this->assign("username", strlen($this->userinfo['username']) > 8 ? name_formart($this->userinfo['username']) : $this->userinfo['username']);
        $this->assign("current_month", $this_year."年".$this_month."月");
        $this->assign("pre_sign_days", $pre_sign_days ? substr($pre_sign_days, 0,-1) : "");
        $this->assign("current_sign_days", $current_sign_days ? substr($current_sign_days, 0,-1) : "");
        $this->assign("mark_flag", $this->user_info['mark_time'] ? (date("Y-m-d",$this->user_info['mark_time']) == date("Y-m-d") ? 0 : 1) : 1);
        $this->assign("calendar", $calendar);
        $this->display('user/sign.html');
    }
    
    //夺宝中
    function duobao(){
        $pageIndex = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $totalCount = $this->db->query("select r.* from tan_go_member_go_record r left join tan_go_shoplist s on s.id = r.shopid where r.uid = ".$this->userinfo['user_id']." and (s.q_uid is null or s.q_showtime = 'Y') group by shopid order by id desc")->num_rows();
        $pageSize = 12;
        $page = new Paging($totalCount,$pageSize);
        $list = $this->db->query("select r.shopid,r.shopqishu,s.q_showtime from tan_go_member_go_record r left join tan_go_shoplist s on s.id = r.shopid  where r.uid = ".$this->userinfo['user_id']." and (s.q_uid is null or s.q_showtime = 'Y') group by r.shopid order by r.id desc limit ".(($pageIndex - 1)*$pageSize).",".$pageSize)->result_array();
        foreach ($list as $key=>$val){
            $shop = $this->db->query("select s.title,s.thumb,s.qishu,s.zongrenshu,s.canyurenshu,s.q_showtime,s.shenyurenshu,s.q_end_time,s.q_user_code,s.q_uid,u.nickname,u.head_img from tan_go_shoplist s left join tan_users_info u on u.user_id = s.q_uid where s.id = $val[shopid] and s.qishu = $val[shopqishu] order by s.id desc limit 1")->row_array();
            //参与总人次
            $canyurenci = $this->db->query("select SUM(gonumber) num from tan_go_member_go_record where uid = ".$this->userinfo['user_id']." and shopid = $val[shopid] and shopqishu = $val[shopqishu]")->row_array();
            
            $list[$key]['title'] = $shop['title'];
            $list[$key]['thumb'] = $shop['thumb'];
            $list[$key]['user_canyurenshu'] = $canyurenci['num'];//用户参与人数
            $list[$key]['zongrenshu'] = $shop['zongrenshu'];//总需求人次
            $list[$key]['q_showtime'] = $shop['q_showtime'];
            $list[$key]['lottery_url'] = www_base_url("lottery/".  encrypt_lottery_id($val['shopid']));
            $list[$key]['shengyurenshu'] = $shop['shenyurenshu'];//剩余人次
            $list[$key]['canyurenshu'] = $shop['canyurenshu'];//总参与人数
            $list[$key]['percent'] = intval((number_format(floatval($shop['canyurenshu'])/$shop['zongrenshu'],2))* 100)."%";
            $list[$key]['shopid'] = encrypt_lottery_id($val['shopid']);
        }
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->assign("type", 1);
        $this->display('user/duobao.html');
    }
    
    //已揭晓
    function  publish(){
        $pageIndex = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $totalCount = $this->db->query("select r.* from tan_go_member_go_record r left join tan_go_shoplist s on s.id = r.shopid where r.uid = ".$this->userinfo['user_id']." and s.q_uid is not null and s.q_showtime = 'N' group by shopid order by id desc")->num_rows();
        $pageSize = 12;
        $page = new Paging($totalCount,$pageSize);
        $list = $this->db->query("select r.shopid,r.shopqishu,s.q_showtime from tan_go_member_go_record r left join tan_go_shoplist s on s.id = r.shopid  where r.uid = ".$this->userinfo['user_id']."  and s.q_uid is not null and s.q_showtime = 'N' group by r.shopid order by s.end_time desc limit ".(($pageIndex - 1)*$pageSize).",".$pageSize)->result_array();
        foreach ($list as $key=>$val){
            $shop = $this->db->query("select s.title,s.thumb,s.qishu,s.zongrenshu,s.canyurenshu,s.q_showtime,s.shenyurenshu,s.q_end_time,s.q_user_code,s.q_uid,u.nickname,u.head_img from tan_go_shoplist s left join tan_users_info u on u.user_id = s.q_uid where s.id = $val[shopid] and s.qishu = $val[shopqishu] order by s.id desc limit 1")->row_array();
            //参与总人次
            $canyurenci = $this->db->query("select SUM(gonumber) num from tan_go_member_go_record where uid = ".$this->userinfo['user_id']." and shopid = $val[shopid] and shopqishu = $val[shopqishu]")->row_array();
            
            $list[$key]['title'] = $shop['title'];
            $list[$key]['thumb'] = $shop['thumb'];
            $list[$key]['user_canyurenshu'] = $canyurenci['num'];
            $list[$key]['zongrenshu'] = $shop['zongrenshu'];
            $list[$key]['q_showtime'] = $shop['q_showtime'];
            $list[$key]['lottery_url'] = www_base_url("lottery/".  encrypt_lottery_id($val['shopid']));
            $list[$key]['nickname'] = $shop['nickname'];
            $list[$key]['head_img'] = $shop['head_img'];
            $list[$key]['luck_code'] = $shop['q_user_code'];
            $list[$key]['space_url'] = www_base_url("u/".encrypt_user_id($shop['q_uid']));
            //获奖用户在本期参与多少次
            $canyurenci = $this->db->query("select SUM(gonumber) num from tan_go_member_go_record where uid = $shop[q_uid] and shopid = $val[shopid] and shopqishu = $val[shopqishu]")->row_array();
            $list[$key]['lucker_canyurenci'] = $canyurenci['num'];
            $list[$key]['time'] = date("Y-m-d H:i:s",substr($shop['q_end_time'],0,10)).substr($shop['q_end_time'],10,13);
            $list[$key]['shopid'] = encrypt_lottery_id($val['shopid']);
        }
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->assign("type", 2);
        $this->assign("action", "duobao");
        $this->display('user/publish.html');
    }
    
    //中奖记录
    function  winner(){
        $pageIndex = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $totalCount = $this->db->query("select s.* from tan_go_shoplist s where s.q_uid = ".$this->userinfo['user_id']." and s.q_showtime = 'N' order by s.end_time desc")->num_rows();
        $pageSize = 12;
        $page = new Paging($totalCount,$pageSize);
        $list = $this->db->query("select s.id,s.title,s.thumb,s.qishu,s.q_user_code,s.q_end_time,s.q_showtime,s.post_code,s.post_company from tan_go_shoplist s where  s.q_uid = ".$this->userinfo['user_id']." and s.q_showtime = 'N' order by s.end_time desc limit ".(($pageIndex - 1)*$pageSize).",".$pageSize)->result_array();
        foreach ($list as $key=>$val){
            //参与总人次
            $canyurenci = $this->db->query("select SUM(gonumber) num from tan_go_member_go_record where uid = ".$this->userinfo['user_id']." and shopid = $val[id] and shopqishu = $val[qishu]")->row_array();
            $list[$key]['user_canyurenshu'] = $canyurenci['num'];
            $list[$key]['lottery_url'] = www_base_url("lottery/".  encrypt_lottery_id($val['shopid']));
            $list[$key]['time'] = date("Y-m-d H:i:s",substr($val['q_end_time'],0,10)).substr($val['q_end_time'],10,13);
            $list[$key]['shopid'] = encrypt_lottery_id($val['id']);
        }
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->assign("type", 3);
        $this->assign("action", "duobao");
        $this->display('user/winner.html');
    }
    
    function doexchange(){
        if($_POST){
            $type = trim($_POST['type']);//产品类型en实物、vir虚拟
            $gift_id = trim($_POST['proId']);//商品id
            $payPass = trim($_POST['payPass']);//交易密码
            
            if(!$payPass){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "请输入正确的交易密码";
                echo JSON($mess);exit;
            }
            if($this->user_info['paypassword'] != md5(md5($payPass) .$this->user_info['salt'])){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "请输入正确的交易密码";
                echo JSON($mess);exit;
            }
            
            if(!$gift_id){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "您兑换的商品不存在";
                echo JSON($mess);exit;
            }
            $gift = $this->gift_model->get($gift_id);
            if(!$gift){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "您兑换的商品不存在";
                echo JSON($mess);exit;
            }
            //验证余额
            if($gift['amount'] - $this->user_info['balance'] > 0){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1006;
                $mess['errorMsg'] = "您的溜溜币余额不足";
                echo JSON($mess);exit;
            }
            $exchange_order = array();
            if($type == 'en'){
                $type = 1;//实物
                $name = trim($_POST['name']);//收货人姓名
                $phone = trim($_POST['phone']);//收货人电话
                $address = trim($_POST['address']);//收货人地址
                if(!$name || !$phone || !$address){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1000;
                    $mess['errorMsg'] = "发货信息填写不完善";
                    echo JSON($mess);exit;
                }
                $exchange_order['name'] = $name;
                $exchange_order['phone'] = $phone;
                $exchange_order['address'] = $address;
                
            }elseif($type == 'vir'){
                $type = 2;//虚拟
                $account = trim($_POST['account']);//账号
                if(!$account){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1000;
                    $mess['errorMsg'] = "发货信息填写不完善";
                    echo JSON($mess);exit;
                }
                $exchange_order['account'] = $account;
            }
            $exchange_order['user_id'] = $this->userinfo['user_id'];
            $exchange_order['order_nid'] = MakeOrdId($this->userinfo['user_id']);//生成订单
            $exchange_order['gift_id'] = $gift_id;
            $exchange_order['title'] = $gift['title'];
            $exchange_order['amount'] = $gift['amount'];
            $exchange_order['type'] = $type;
            $exchange_order['num'] = 1;
            $exchange_order['status'] = 2;
            $exchange_order['create_time'] = time();
            $result = $this->exchange_order_model->insert($exchange_order);
            if($result){
                //兑换记录
                $exchange_list_data = array();
                $exchange_list_data['user_id'] = $exchange_order['user_id'];
                $exchange_list_data['order_nid'] = $exchange_order['order_nid'];
                $exchange_list_data['title'] = $exchange_order['title'];
                $exchange_list_data['amount'] = $exchange_order['amount'];
                $exchange_list_data['type'] = $exchange_order['type'];
                $exchange_list_data['num'] = $exchange_order['num'];
                $exchange_list_data['status'] = $exchange_order['status'];
                $exchange_list_data['create_time'] = $exchange_order['create_time'];
                $this->exchange_list_model->insert($exchange_list_data);
                
                //获取用户账户信息
                $account = $this->account_model->get($this->userinfo['user_id']);
                $account_data = array();
                $account_data['balance'] = $account['balance'] - $gift['amount'];//账户余额
                $account_data['expend'] = $account['expend'] + $gift['amount'];//总支出
                //更新账户
                $this->account_model->update($account_data,$this->userinfo['user_id']);
                
                //弹溜溜明细
                $finance_list_data = array();
                $finance_list_data['user_id'] = $this->userinfo['user_id'];
                $finance_list_data['content'] = '兑换商品【'.$exchange_order['title']."】";
                $finance_list_data['finance'] = $exchange_order['amount'];
                $finance_list_data['finance_balance'] = $account_data['balance'];
                $finance_list_data['reward_type'] = 'exchange_deduct';
                $finance_list_data['type'] = 2;//支出
                $finance_list_data['create_time'] = time();
                $finance_list_data['month'] = date("Ym");
                $finance_list_data['day'] = date("Ymd");
                $this->finance_list_model->insert($finance_list_data);
                
                //日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'exchange';
                $log_data['content'] = '申请兑换';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "恭喜您，兑换成功！";
                echo JSON($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1003;
                $mess['errorMsg'] = "兑换失败";
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    //领取体验奖励
    function do_receive(){
        if($_POST){
            
            $SSID = isset($_POST['keyCode']) ? trim($_POST['keyCode']) : "";
            if($SSID != $this->session_id){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            $award_ids = isset($_POST['award_ids']) ? trim($_POST['award_ids']) : "";
            if(!$award_ids){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "参数传入有误";
                echo JSON($mess);exit;
            }
            
            //验证是否首次领取奖励
            $flag = $this->plat_award_list_model->getCount(array("user_id"=>$this->user_info['user_id'],"is_receive"=>1));
            
            $postData = array();
            $postData['user_id'] = $this->user_info['user_id'];
            $postData['award_ids'] = $award_ids;
            
            $recieve_interface_link = $this->remote_url['recieve_url'];//领取体验奖励接口
            $result = $this->crypt->POST_Api($recieve_interface_link,$postData);//发送post查询请求
            $result = json_decode($result,true);
            if($result && $result['result'] != null && $result['result'] == 1){
                
                if(!$flag){
                    $this->new_task = $this->config_task['new_task'];
                    $finance_award = $this->new_task[2]['award_finance'];
                    $experience_award = $this->new_task[2]['award_experience'];
                    
                    $new_task_step = array();
                    $new_task_step['is_finish'] = 1;
                    $new_task_step['finish_time'] = time();
                    $this->new_task_model->update($new_task_step,array('user_id'=>$this->userinfo['user_id'],'step'=>3));
                    
                    //奖励通知
                    $message = '恭喜您完成新手任务第三步【'.$this->new_task[2]['title'].'】，奖励您'.$finance_award."溜溜币和".$experience_award."积分。感谢您对弹溜溜的支持！";
                    $this->db->insert("users_award_notice", array('user_id'=>$this->userinfo['user_id'],'title'=>'任务通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                }
                $mess['reqStatus'] = true;
                $mess['errorMsg'] = "领取成功，溜溜币+<span style='color:red;font-size:18px;'>".$result['award_total']."</span>".($result['exper_total'] ? "，积分+<span style='color:red;font-size:18px;'>".$result['exper_total']."</span>" : "");
                $mess['award_total'] = $result['award_total'];
                $mess['exper_total'] = $result['exper_total'];
                if(isset($_POST['plat_id'])){
                    $mess['url'] = site_url("mplat_".$_POST['plat_id']);
                }
                echo JSON($mess);exit;
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = $result['info'];            
                echo JSON($mess);exit;
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1001;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    //签到
    function do_mark(){
        if($_POST){
            $mark_award = $this->config_award['mark_award'];
            $mess = array();
            //奖励溜溜币
            $amount = $award_amount = rand($mark_award['min'],$mark_award['max']);
            //奖励积分
            $experience = $award_experience = rand($mark_award['min'],$mark_award['max']);
            $vip_degree = $this->user_info['vip_degree'];
            if($vip_degree){//会员奖励
                $vip_amount = intval($award_amount*$mark_award[$vip_degree]);
                $vip_experience = intval($award_experience*$mark_award[$vip_degree]);
                $amount = intval($award_amount+$vip_amount);
                $experience = intval($award_experience+$vip_experience);
            }
            
            //上一次签到时间
            $mark_time = $this->user_info['mark_time'];
            
            //判断是否已经签到了
            if(date("Ymd",$mark_time) == date('Ymd')){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                echo JSON($mess);exit; 
            }
            
            //连续签到次数
            $mark_count = $this->user_info['mark_count'];
            if(!$mark_time){//第一次签到
                $mark_time = time();
                $mark_count = 1;
            }elseif(date("Ymd",$mark_time) == date("Ymd",  strtotime("-1 day"))){//连续签到
                $mark_time = time();
                $mark_count = $mark_count + 1;
            }else{//非连续签到
                $mark_time = time();
                $mark_count = 1;
            }
            
            
            $msg_finance = "签到奖励".$amount."溜溜币";
            $msg_experience = "签到奖励".$experience."积分";
            //获取账户信息
            $account = $this->account_model->get($this->userinfo['user_id']);
            $account_data = array();
            $account_data['total'] = $account['total']+ $amount;//账户总额
            $account_data['balance'] = $account['balance'] + $amount;//账户余额
//            $account_data['experience'] = $account['experience'] + $experience;//当前积分值
//            $account_data['experience_balance'] = $account['experience_balance'] +  $experience;//可用积分值
            //更新账户
            $result = $this->account_model->update($account_data,$this->userinfo['user_id']);
            if($result){
                //更新用户签到信息
                $data = array();
                $data['mark_time'] = $mark_time;
                $data['mark_count'] = $mark_count;
                $data['lucky_count'] = $this->user_info['lucky_count'] + 1;
                $this->users_model->user_update($data,  $this->userinfo['user_id']);
                        
                //写入签到记录
                $sign_data = array();
                $sign_data["user_id"] = $this->userinfo['user_id'];
                $sign_data["amount"] = $award_amount;
                $sign_data["amount_vip"] = $vip_amount ? $vip_amount : 0;
                $sign_data["exper"] = $award_experience;
                $sign_data["exper_vip"] = $vip_experience ?$vip_experience : 0;
                $sign_data['year_time'] = date("Y");
                $sign_data["month_time"] = date("m");
                $sign_data["day_time"] =date("d");
                $sign_data['create_time'] = time();
                $this->users_model->insert_user_sign($sign_data);
                
                //写入溜溜币明细
                $finance_list_data = array();
                $finance_list_data['user_id'] = $this->userinfo['user_id'];
                $finance_list_data['content'] = $msg_finance;
                $finance_list_data['finance'] = $amount;
                $finance_list_data['finance_balance'] = $account_data['balance'];
                $finance_list_data['reward_type'] = 'sign_award';
                $finance_list_data['type'] = 1;
                $finance_list_data['create_time'] = time();
                $finance_list_data['month'] = date("Ym");
                $finance_list_data['day'] = date("Ymd");
                $this->finance_list_model->insert($finance_list_data);
//                //写入积分明细
//                $experience_list_data = array();
//                $experience_list_data['user_id'] = $this->userinfo['user_id'];
//                $experience_list_data['content'] = $msg_experience;
//                $experience_list_data['experience'] = $experience;
//                $experience_list_data['experience_balance'] = $account_data['experience_balance'];
//                $experience_list_data['reward_type'] = 'sign_award';
//                $experience_list_data['type'] = 1;
//                $experience_list_data['create_time'] = time();
//                $experience_list_data['month'] = date("Ym");
//                $experience_list_data['day'] = date("Ymd");
//                $this->experience_list_model->insert($experience_list_data);
                
                //日志
                $log_data = array();
                $log_data['user_id'] = $this->userinfo['user_id'];
                $log_data['events'] = 'sign';
                $log_data['content'] = '签到成功';
                $log_data['ip'] = $this->input->ip_address();
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                $mess['status'] = '0';
                $mess['coin'] = $award_amount;
                $mess['expe'] = 0;//$award_experience
                $mess['vip_coin'] = $vip_amount ? $vip_amount : 0;
                $mess['vip_expe'] = $vip_experience ?$vip_experience : 0;
                $mess['mark_count'] = $mark_count;
                $mess['reqStatus'] = true;
                echo JSON($mess);exit;
            }
        }
    }
}
